﻿using System;
using System.Collections;
using System.Data;
namespace GeneralLibs.Lists
{
    public class ChiTietPhieuUpKid : DoiTuong
    {
        #region Phieu property
        public Phieu Phieu
        {
            get
            {
                if (_DataRow["PhieuID"] is Guid)
                    return (Phieu)DanhMucPhieu.Instance.GetByID((Guid)_DataRow["PhieuID"]);
                else return null;
            }
            set
            {
                if (value is Phieu)
                    _DataRow["PhieuID"] = value.ID;
                else
                    _DataRow["PhieuID"] = DBNull.Value;
            }
        }
        #endregion

        #region int MaCau property
        public int MaCau
        {
            get
            {
                if (_DataRow["MaCau"] is int) return (int)_DataRow["MaCau"];
                return -1;
            }
            set
            {
                _DataRow["MaCau"] = value;
            }
        }
        #endregion

        #region LevelTraLoi property
        public int LevelTraLoi
        {
            get
            {
                if (_DataRow["LevelTraLoi"] is int) return (int)_DataRow["LevelTraLoi"];
                else return -1;
            }
            set
            {
                _DataRow["LevelTraLoi"] = value;
            }
        }
        #endregion

        #region string CauTraLoiFIX1 property
        public string CauTraLoiFIX1
        {
            get
            {
                if (_DataRow["CauTraLoiFIX1"] is string) return (string)_DataRow["CauTraLoiFIX1"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX1"] = value;
            }
        }
        #endregion

        #region string CauTraLoiFIX2 property
        public string CauTraLoiFIX2
        {
            get
            {
                if (_DataRow["CauTraLoiFIX2"] is string) return (string)_DataRow["CauTraLoiFIX2"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX2"] = value;
            }
        }
        #endregion

        #region string CauTraLoiFIX3 property
        public string CauTraLoiFIX3
        {
            get
            {
                if (_DataRow["CauTraLoiFIX3"] is string) return (string)_DataRow["CauTraLoiFIX3"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX3"] = value;
            }
        }
        #endregion

        #region string CauTraLoiFIX4 property
        public string CauTraLoiFIX4
        {
            get
            {
                if (_DataRow["CauTraLoiFIX4"] is string) return (string)_DataRow["CauTraLoiFIX4"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiFIX4"] = value;
            }
        }
        #endregion

        #region string CauTraLoiText1 property
        public string CauTraLoiText1
        {
            get
            {
                if (_DataRow["CauTraLoiText1"] is string) return (string)_DataRow["CauTraLoiText1"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText1"] = value;
            }
        }
        #endregion

        #region string CauTraLoiText2 property
        public string CauTraLoiText2
        {
            get
            {
                if (_DataRow["CauTraLoiText2"] is string) return (string)_DataRow["CauTraLoiText2"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText2"] = value;
            }
        }
        #endregion

        #region string CauTraLoiText3 property
        public string CauTraLoiText3
        {
            get
            {
                if (_DataRow["CauTraLoiText3"] is string) return (string)_DataRow["CauTraLoiText3"];
                return "";
            }
            set
            {
                _DataRow["CauTraLoiText3"] = value;
            }
        }
        #endregion

        #region DateTime? ThoiDiem1 property
        public DateTime? ThoiDiem1
        {
            get
            {
                if (_DataRow["ThoiDiem1"] is DateTime)
                    return (DateTime)_DataRow["ThoiDiem1"];
                else
                    return null;
            }
            set
            {
                _DataRow["ThoiDiem1"] = value;
            }
        }
        #endregion

        #region DateTime? ThoiDiem2 property
        public DateTime? ThoiDiem2
        {
            get
            {
                if (_DataRow["ThoiDiem2"] is DateTime)
                    return (DateTime)_DataRow["ThoiDiem2"];
                else
                    return null;
            }
            set
            {
                _DataRow["ThoiDiem2"] = value;
            }
        }
        #endregion

        #region DateTime? ThoiDiem3 property
        public DateTime? ThoiDiem3
        {
            get
            {
                if (_DataRow["ThoiDiem3"] is DateTime)
                    return (DateTime)_DataRow["ThoiDiem3"];
                else
                    return null;
            }
            set
            {
                _DataRow["ThoiDiem3"] = value;
            }
        }
        #endregion

        #region string GhiChu property
        public string GhiChu
        {
            get
            {
                if (_DataRow["GhiChu"] is string) return (string)_DataRow["GhiChu"];
                return "";
            }
            set
            {
                _DataRow["GhiChu"] = value;
            }
        }
        #endregion

        public ChiTietPhieuUpKid(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucChiTietPhieuUpKid : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucChiTietPhieuUpKid _Instance;
        public static DanhMucChiTietPhieuUpKid Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucChiTietPhieuUpKid();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "ChiTietPhieuUpKid";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new ChiTietPhieuUpKid((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public ChiTietPhieuUpKid this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new ChiTietPhieuUpKid(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public ChiTietPhieuUpKid this[int indexView, bool a]
        {
            get
            {
                if (0 <= indexView && indexView <= this.Count)
                {
                    return new ChiTietPhieuUpKid(DataView.ToTable().Rows[indexView]);
                }
                else
                {
                    return null;
                }
            }
        }

        public ChiTietPhieuUpKid NewDoiTuong()
        {
            return new ChiTietPhieuUpKid(DataTable.NewRow());
        }

        public ChiTietPhieuUpKid GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new ChiTietPhieuUpKid(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public ChiTietPhieuUpKid GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucChiTietPhieuUpKid GetAll()
        {
            return Instance;
        }

        public static DanhMucChiTietPhieuUpKid Filter(string filterExpression)
        {
            try
            {
                DanhMucChiTietPhieuUpKid obj = new DanhMucChiTietPhieuUpKid();
                obj.DataView = new DataView(DanhMucChiTietPhieuUpKid.Instance.DataTable, filterExpression,"PhieuID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static ChiTietPhieuUpKid GetByPhieuID_MaCau_One(Guid phieuID, int maCau)
        {
            try
            {
                DanhMucChiTietPhieuUpKid dm = Filter(string.Format("PhieuID = '{0}' AND MaCau = {1}", phieuID, maCau));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucChiTietPhieuUpKid GetByPhieuID_MaCau_Many(Guid phieuID, int maCau)
        {
            try
            {
                DanhMucChiTietPhieuUpKid dm = Filter(string.Format("PhieuID = '{0}' AND MaCau = {1}", phieuID, maCau));
                return dm;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static ChiTietPhieuUpKid GetByPhieuID_MaCau_Level(Guid phieuID, int maCau, int levelTraLoi)
        {
            try
            {
                DanhMucChiTietPhieuUpKid dm = Filter(string.Format("PhieuID = '{0}' AND MaCau = {1} AND LevelTraLoi = {2}", phieuID, maCau, levelTraLoi));
                if (dm.Count >= 1) return dm[0, true];
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucChiTietPhieuUpKid GetByPhieuID(Guid phieuID)
        {
            try
            {
                return Filter(string.Format("PhieuID = '{0}'", phieuID));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
